﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using tinyweb.framework;
using Bepo.Model.Data;
using Bepo.Helpers;

namespace Bepo.Handlers.API
{
    public class ApiBusinessmodelsHandler
    {
        public IResult Get()
        {
            var items = BepoDB.BusinessModels
                .All()
                .Select(x => new { Id=x.Id, Name = x.Name }).ToList();
            return Envelope.Json(items);
        }

        public IResult Post(string Name, string Description)
        {
            var existing = BepoDB.BusinessModels.Where("Name = @0", Name.Trim()).FirstOrDefault();
            if (existing != null) return Envelope.Error("Entity already existing");
            BusinessModelRecord.Create(Name.Trim(), Description.Trim());
            return Envelope.None();
        }
    }

    public class ApiBusinessmodelHandler
    {
        public IResult Get(Guid id)
        {
            var item = BepoDB.BusinessModels.Where("Id = @0", id).FirstOrDefault();
            return Envelope.Json(item);
        }
    }
}